
// 电影相关功能
document.addEventListener('DOMContentLoaded', function () {
    // 加载电影列表
    let maxPage= 1
    loadMovies().then( (value) => {
        maxPage = value;
    });

    // 加载排行榜
    loadRankings();

    // 电影筛选功能
    const genreFilter = document.getElementById('genre-filter');
    const regionFilter = document.getElementById('region-filter');
    const sortFilter = document.getElementById('sort-filter');

    if (genreFilter && regionFilter && sortFilter) {
        [genreFilter, regionFilter, sortFilter].forEach(filter => {
            filter.addEventListener('change', function () {
                loadMovies();
            });
        });
    }

    // 分页功能
    const prevPageBtn = document.getElementById('prev-page');
    const nextPageBtn = document.getElementById('next-page');
    const pageInfo = document.getElementById('page-info');

    if (prevPageBtn && nextPageBtn && pageInfo) {
        let currentPage = 1;
        const pageSize = 10; // 每页显示的电影数量

        prevPageBtn.addEventListener('click', function () {
            if (currentPage > 1) {
                currentPage--;
                loadMovies(currentPage, pageSize);
                updatePageInfo();
            }
        });

        nextPageBtn.addEventListener('click', function () {
            if (currentPage >= maxPage) {
                return;
            }
            currentPage++;
            loadMovies(currentPage, pageSize);
            updatePageInfo();
        });

        function updatePageInfo() {
            pageInfo.textContent = `第${currentPage}页`;
        }
    }
});

// 加载电影列表
async function loadMovies(page = 1,pageSize = 10) {
    const genre = document.getElementById('genre-filter').value;
    const region = document.getElementById('region-filter').value;
    const sort = document.getElementById('sort-filter').value;
    let maxPage=0;
    try {
        const movies = await getMovies(genre, region, sort);
        maxPage=movies.length/pageSize;
        const moviesOnPage = movies.slice((page - 1) * pageSize, page * pageSize);
        renderMovies(moviesOnPage);
    } catch (error) {
        console.error('加载电影列表错误:', error);
        alert('加载电影列表失败');
    }
    return maxPage;
}

function renderMovies(movies) {
    const movieGrid = document.getElementById('movie-grid');
    if (!movieGrid) return;

    if (movies.length === 0) {
        movieGrid.innerHTML = '<p class="no-results">没有找到匹配的电影</p>';
        return;
    }

    movieGrid.innerHTML = movies.map(movie => `
        <div class="movie-card" onclick="window.location.href='/movie/detail?id=${movie.id}'">
            <div class="movie-poster">
                <img src="/image/${movie.coverUrl}" alt="${movie.title}海报">
                <div class="movie-vip-badge ${movie.vip ? 'vip' : 'free'}">${movie.vip ? 'VIP' : '免费'}</div>
            </div>
            <div class="movie-info">
                <h3 class="movie-title">${movie.title}</h3>
                <div class="movie-meta">
                    <span><i class="fas fa-star"></i> ${movie.rating}</span>
                    <span><i class="fas fa-calendar"></i> ${movie.releaseDate.substring(0, 4)}</span>
                </div>
            </div>
        </div>
    `).join('');
}

// 模拟获取电影函数
async function getMovies(genre, region, sort) {
    let movies;
    console.log("getMovies");
    const urlParams = new URLSearchParams(window.location.search);
    // 获取单个参数
    const name = urlParams.get('query');
    const response = await axios.get("/movie/searchQuery?query="+name);
    movies = response.data;

    // 简单的筛选逻辑
    let filteredMovies = [...movies];

    if (genre !== 'all') {
        filteredMovies = filteredMovies.filter(movie => movie.type.includes(genre));
    }

    if (region !== 'all') {
        filteredMovies = filteredMovies.filter(movie => movie.region === region);
    }

    // 简单的排序逻辑
    switch (sort) {
        case 'popularity':
            filteredMovies.sort((a, b) => b.rating - a.rating);
            break;
        case 'rating':
            filteredMovies.sort((a, b) => b.rating - a.rating);
            break;
        case 'date':
            filteredMovies.sort((a, b) => b.year - a.year);
            break;
    }

    return filteredMovies;
}